package com.ruoyi.web.controller.crm;

import com.bonuo.crm.domain.Year;
import com.bonuo.crm.service.YearService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/crm/year")
@Api(tags = "财年分配管理")
public class YearController extends BaseController {
    @Autowired
    private YearService yearService;

    //查询财年列表
    @GetMapping("/list")
    @ApiOperation("获取财年列表")
    public TableDataInfo list(Year year,Integer pageNum,Integer pageSize){
        if(pageNum != null && pageSize != null){
            startPage();
        }
        List<Year> yearList = yearService.selectYearList(year);
        return getDataTable(yearList);
    }

    //添加财年
    @PostMapping("/add")
    @ApiOperation("添加财年")
    public AjaxResult add(@RequestBody Year year){
        //先查询年份是否存在
        Integer yearByYear = yearService.selectYearByYear(year.getYear());
        if(yearByYear >= 1){
            return AjaxResult.error("该财年已存在");
        }
        return toAjax(yearService.add(year));
    }

    //修改财年
    @PutMapping("/update")
    @ApiOperation("修改财年")
    public AjaxResult update(@RequestBody Year year){
        return toAjax(yearService.update(year));
    }

    //删除财年
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除财年")
    public AjaxResult delete(@PathVariable Integer id){
        return toAjax(yearService.delete(id));
    }
}
